<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="newsList">
      <el-table-column label="主图" prop="img" width="140">
        <template slot-scope="scope">
          <img :src="scope.row.img" width="120" height="60"/>
        </template>
      </el-table-column>
      <el-table-column label="主题" prop="title">
        <template slot-scope="scope">
          <div >{{ scope.row.title }}</div>
          <div style="font-size: 12px;color: #C9CDD4;">
            阅读：{{ scope.row.read }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="简短介绍" prop="introduce">
        <template slot-scope="scope">
          <div class="introduce-div">
            {{ scope.row.introduce }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="公众号链接" prop="link"/>
      <el-table-column label="创建时间" prop="createAt" width="120"/>
      <el-table-column label="操作" class-name="small-padding fixed-width" width="140">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改行业资讯对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="主题" prop="title">
          <el-input v-model="form.title" placeholder="请输入主题"/>
        </el-form-item>
        <el-form-item label="主图" prop="img">
          <ImageUpload v-model="form.img" :limit="1"/>
        </el-form-item>
        <el-form-item label="简短介绍" prop="introduce">
          <el-input type="textarea" rows="4" v-model="form.introduce" placeholder="请输入简短介绍"/>
        </el-form-item>
        <el-form-item label="详情介绍" prop="detail">
          <Editor v-model="form.detail" :height="300"/>
        </el-form-item>
        <el-form-item label="公众号链接" prop="link">
          <el-input v-model="form.link" placeholder="请输入公众号链接"/>
        </el-form-item>
        <el-form-item label="阅读次数" prop="read">
          <el-input v-model="form.read" disabled placeholder="请输入阅读次数"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="small">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNews, getNews, delNews, addNews, updateNews } from '@/api/app/news'

export default {
  name: 'News',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 行业资讯表格数据
      newsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: '主题不能为空', trigger: 'blur' }
        ],
        img: [
          { required: true, message: '主图不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '副标题不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询行业资讯列表 */
    getList() {
      this.loading = true
      listNews(this.queryParams).then(response => {
        this.newsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        img: null,
        introduce: null,
        detail: null,
        link: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加行业资讯'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getNews(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改行业资讯'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNews(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addNews(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除行业资讯编号为"' + ids + '"的数据项？').then(function() {
        return delNews(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.introduce-div {
  background: #f5f5f5;
  padding: 10px;
  border-radius: 5px;
  font-size: 12px;
}
</style>
